// pages/bgImages/bgImages.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 前台显示list
    items: [],
    // 当前页
    pageNumber: 0,
    // 总页数
    totalPage: 1,
    checkedID: null,
    url: '',
    tempFilePaths: '',
    isimg: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.request({
      url: 'https://wsh.weyifeng.com/allBgImages',
      data: { 'page': that.data.pageNumber },
      header: { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' },
      success: function (res) {
        console.log(res.data)
        that.setData({
          items: res.data.content,
          totalPage: res.data.totalPages,
        })
      }
    })
  },
  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    var that = this;
    // 当前页+1
    var pageNumber = that.data.pageNumber + 1;

    that.setData({
      pageNumber: pageNumber,
    })

    if (pageNumber <= that.data.totalPage) {
      wx.showLoading({
        title: '加载中',
      })
      // 请求后台，获取下一页的数据。
      wx.request({
        url: 'https://wsh.weyifeng.com/allBgImages',
        data: {
          page: pageNumber,
        },
        success: function (res) {
          wx.hideLoading()
          // 将新获取的数据 res.data.list，concat到前台显示的showlist中即可。
          that.setData({
            items: that.data.items.concat(res.data.content)
          })
        },
        fail: function (res) {
          wx.hideLoading()
        }
      })
    } else {
      that.setData({
        isimg: true
      })
    }

  },
  check: function (e) {
    console.log(e)
    let id = e.currentTarget.dataset.id;
    let url = e.currentTarget.dataset.url;

    if (app.globalData.isVip == '1') {
      if (this.data.checkedID !== id) {
        console.log("Id" + this.data.checkedID)
        console.log("id" + id)
        this.setData({
          checkedID: id,
          url: url,
        })
      }
    } else {
      if (id !== 1 && id !== 2 && id !== 3) {
        wx.showModal({
          content: '只能免费选择前三哦。支付1元，可以使用本程序所有功能，是否支付？',
          cancelText: '否',
          confirmText: '是',
          success: function (res) {
            if (res.confirm) {
              app.vipPay()
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
            console.log("背景非会员提示:" + res.data)
          }
        })
      } else {
        if (this.data.checkedID !== id) {
          console.log("Id" + this.data.checkedID)
          console.log("id" + id)
          this.setData({
            checkedID: id,
            url: url,
          })
        }
      }
    }
  },
  /*save: function (e) {
    let id = this.data.checkedID;
    let url = this.data.url;
    app.globalData.bgImages = url
    wx.navigateBack()
  },*/
  onUnload:function(){
    let id = this.data.checkedID;
    let url = this.data.url;
    app.globalData.bgImages = url
  }
})